﻿// Copyright © Microsoft Corporation.  All Rights Reserved.
// This code released under the terms of the 
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;

namespace TheOliver.Controls
{
    public class Animation
    {
        // this method will create a new storyboard and start the animation
        public static void playAnimation(string element, string property, TimeSpan time, double value)
        {
            // create a new storyboard for the animation and set the targetname and property
            Storyboard sb = new Storyboard();
            sb.SetValue(Storyboard.TargetNameProperty, element);
            sb.SetValue(Storyboard.TargetPropertyProperty, new PropertyPath(property));

            // create a DoubleAnimation for the storyboard, set the properties and add it to the storyboard
            DoubleAnimation animation = new DoubleAnimation();
            animation.Duration = time;
            animation.To = value;
            sb.Children.Add(animation);

            // add the animation to the resources -> required to play the animation
            ((UserControl)Application.Current.RootVisual).Resources.Remove("sb");
            ((UserControl)Application.Current.RootVisual).Resources.Add("sb", sb);

            // finally start the animation
            sb.Begin();
        }
    }
}
